package com.test.dababy.dababy_08_loan_repay;

import static com.test.dababy.dababy_00_utils.DababyUtil.startLoading;
import static com.test.dababy.dababy_00_utils.DababyUtil.stopLoading;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.dababy.R;
import com.test.dababy.dababy_00_customer_service.Customer_dababyServiceDialog;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_04_home.MainAct_dababyivity;
import com.test.dababy.dababy_05_process.Pro_dababycessIn_dababyfo;
import com.test.dababy.dababy_07_loan_status.BaseLoanFragment;
import com.test.dababy.dababy_04_home.LnLi_dababymitInfo;
import com.test.dababy.Othe_dababyrsInfo;
import com.test.dababy.dababy_00_http.OkHttpUtil;
import com.test.dababy.dababy_00_utils.DababyUtil;

public class Ln_dababyRpyFr_dababyagment extends BaseLoanFragment {

    private char uoTE='-';
    private String hYgV="";
    private char x704='T';
    private double e11Q=0.33497058232704724;
    private String v70g="";
    private float e58V=0.2726977f;

    public void cTubI(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(gethYgV())) sethYgV(getV70g()+getUoTE()+getX704());
                else sethYgV(String.valueOf(getE58V())+getE11Q());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setUoTE(char uoTE) {
        this.uoTE = uoTE;
    }

    public void sethYgV(String hYgV) {
        this.hYgV = hYgV;
    }

    public void setX704(char x704) {
        this.x704 = x704;
    }

    public void setE11Q(double e11Q) {
        this.e11Q = e11Q;
    }

    public void setV70g(String v70g) {
        this.v70g = v70g;
    }

    public void setE58V(float e58V) {
        this.e58V = e58V;
    }

    public char getUoTE() {
        return uoTE;
    }

    public String gethYgV() {
        return hYgV;
    }

    public char getX704() {
        return x704;
    }

    public double getE11Q() {
        return e11Q;
    }

    public String getV70g() {
        return v70g;
    }

    public float getE58V() {
        return e58V;
    }

    private TextView homeAmountTv;
    private TextView totalRepaymentTv;
    private TextView dueDateTv;
    private TextView daysRemainingTv;
    private TextView loanAmountTv;
    private TextView interestTv;
    private TextView serviceFeeTv;
    private TextView overdueDaysTv;
    private TextView overduePenaltyTv;
    private View foldView;
    private ImageButton foldIb;
    private Button repayBtn;

    private View vatView;
    private TextView vatTv;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dababy_loan_rpy, container, false);

        homeAmountTv = view.findViewById(R.id.fdlr_tv_home_amount);
        TextView dailyInterestTv = view.findViewById(R.id.fdlr_tv_daily_interest);
        totalRepaymentTv = view.findViewById(R.id.fdlr_tv_total_repayment);
        dueDateTv = view.findViewById(R.id.fdlr_tv_due_date);
        daysRemainingTv = view.findViewById(R.id.fdlr_tv_days_remaining);
        loanAmountTv = view.findViewById(R.id.fdlr_tv_loan_amount);
        interestTv = view.findViewById(R.id.fdlr_tv_interest);
        serviceFeeTv = view.findViewById(R.id.fdlr_tv_service_fee);
        vatView = view.findViewById(R.id.fdlr_view_vat);
        vatTv = view.findViewById(R.id.fdlr_tv_vat);
        overdueDaysTv = view.findViewById(R.id.fdlr_tv_overdue_days);
        overduePenaltyTv = view.findViewById(R.id.fdlr_tv_overdue_penalty);
        foldView = view.findViewById(R.id.fdlr_view_fold);
        vatView = view.findViewById(R.id.fdlr_view_vat);
        foldIb = view.findViewById(R.id.fdlr_ib_fold);
        repayBtn = view.findViewById(R.id.fdlr_btn_repay);

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.fdlr_refresh_layout);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            startLoading(requireContext(),"");
            OkHttpUtil.getInstance_dababy().uploadEventPoint("REPAY","REFRESH_PULL","");

            String state = Pro_dababycessIn_dababyfo.getInstance_dababy().getDababyUtil();
            if (DababyUtil.isStateChange_dababy(state)) {//刷新用户状态
                DababyUtil.stopLoading();
                FragmentActivity activity = requireActivity();
                DababyUtil.clearState_dababy();
                activity.startActivity(new Intent(activity, MainAct_dababyivity.class));
                activity.finish();
            }else {
                OkHttpUtil.getInstance_dababy().fetchRpDetail(this::refreshHomeLimit);
            }
        });

        view.findViewById(R.id.fdlr_ib_cs).setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint("REPAY","CUSTOMER_SERVICE_CLICK","");
            new Customer_dababyServiceDialog(requireContext()).show();
        });

        dailyInterestTv.setText(requireContext().getString(R.string.home_daily_interest_xxx, App_dababyShow_dababyCustomize.getInstance_dababy().getMaxDailyInterest()));

        init_dababy();

        return view;
    }

    private void init_dababy(){
        //折叠/展开
        foldIb.setOnClickListener(v -> fold());

        repayBtn.setOnClickListener(v -> {
            startLoading(requireContext(),"");
            OkHttpUtil.getInstance_dababy().uploadEventPoint("REPAY","SUBMIT_CLICK","");
            if (DababyUtil.isOrderRepayH5_dababy()) OkHttpUtil.getInstance_dababy().odRpH5Path("NO",this::toPay_dababy);
            else if (DababyUtil.isGetOrderRepayH5_dababy()) OkHttpUtil.getInstance_dababy().getOdRpH5Path("NO",this::toPay_dababy);
        });

        cTubI();

    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading(requireContext(),"");
        OkHttpUtil.getInstance_dababy().uploadEventPoint("REPAY","START","");
        OkHttpUtil.getInstance_dababy().requestHomeAmountList(this::refreshHomeLimit);
    }

    private void fold(){

        if (foldView.getVisibility() == View.VISIBLE){
            foldView.setVisibility(View.GONE);
            foldIb.setImageResource(R.drawable.icon_repay_fold);
        }else {
            foldView.setVisibility(View.VISIBLE);
            foldIb.setImageResource(R.drawable.icon_repay_unfold);
        }

    }

    @SuppressLint("SetTextI18n")
    public void refreshHomeLimit(){

        homeAmountTv.setText(LnLi_dababymitInfo.getInstance_dababy().getLimitAmount_dababy()+" "+ LnLi_dababymitInfo.getInstance_dababy().getCurrency_dababy());

        try {
            OkHttpUtil.getInstance_dababy().fetchRpDetail(this::refreshDetails_dababy);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @SuppressLint("SetTextI18n")
    public void refreshDetails_dababy(){

        stopLoading();

        String currency = RpI_dababynf_dababyo.getInstance().getCurrency_dababy();

        totalRepaymentTv.setText(RpI_dababynf_dababyo.getInstance().getRepayAmount_dababy()+" "+currency);
        dueDateTv.setText(RpI_dababynf_dababyo.getInstance().getRepayDate_dababy());
        loanAmountTv.setText(RpI_dababynf_dababyo.getInstance().getAmount_dababy()+" "+currency);
        interestTv.setText(RpI_dababynf_dababyo.getInstance().getInterest_dababy()+" "+currency);
        daysRemainingTv.setText(RpI_dababynf_dababyo.getInstance().getRemainDay());
        overdueDaysTv.setText(RpI_dababynf_dababyo.getInstance().getOverdueDay_dababy());
        overduePenaltyTv.setText(RpI_dababynf_dababyo.getInstance().getOverdueAmount_dababy()+" "+currency);

        if (DababyUtil.isCote_dababy()){
            vatView.setVisibility(View.VISIBLE);
            vatTv.setText(RpI_dababynf_dababyo.getInstance().getServiceVatFee_dababy()+" "+currency);
        }else vatView.setVisibility(View.GONE);

        if (RpI_dababynf_dababyo.getInstance().isOvdu_dababy()) overdueDaysTv.setTextColor(Color.RED);
        else overdueDaysTv.setTextColor(Color.parseColor("#FF8497B4"));

        if (DababyUtil.isFrench_dababy()) serviceFeeTv.setText(RpI_dababynf_dababyo.getInstance().getRepayProFee_dababy()+" "+currency);
        else serviceFeeTv.setText(RpI_dababynf_dababyo.getInstance().getServiceFee_dababy()+" "+currency);

    }

    public void toPay_dababy(){
        stopLoading();
        String path = Othe_dababyrsInfo.getInstance_dababy().getRpPath_dababy();
        if (!TextUtils.isEmpty(path)) startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(path)));
    }
    
    @Override
    public void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("REPAY","END","");
    }

}